package com.ten.mall.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.ten.mall.mapper.AddressMapper;
import com.ten.mall.service.AddressService;
import com.ten.mall.vo.Address;
import com.ten.mall.vo.AddressExample;
import com.ten.mall.vo.AddressExample.Criteria;
@Service
@Transactional
public class AddressServiceImpl implements AddressService {
	@Autowired
	private AddressMapper addressMapper;
	@Override
	@Transactional(readOnly=true)
	public List<Address> selectByPages(Integer start, Integer size, String userId) {
		PageHelper.startPage(start, size);
		AddressExample example =new AddressExample();
		if(userId!=null){
			Criteria c =example.createCriteria();
			c.andUserIdEqualTo(userId);
		}
		List<Address> list =addressMapper.selectByExample(example);
		return list;
	}

	@Override
	public int insertAddress(Address add) {
		if(add.getState()==1){
			addressMapper.updateState(0);
		}
		int i=addressMapper.insert(add);
		return i;
	}

	@Override
	public int updateAddress(Address add) {
		// TODO Auto-generated method stub
		int i  =addressMapper.updateByPrimaryKeySelective(add);
		return i;
	}

	@Override
	public int deleteAddress(Integer id) {
		// TODO Auto-generated method stub
		int i  =addressMapper.deleteByPrimaryKey(id);
		return i;
	}

	@Override
	public Address selectByState(String userId, Integer state) {
		Address add=null;
		AddressExample example =new AddressExample();
		Criteria c =example.createCriteria();
		if(StringUtils.isNotBlank(userId)){
			c.andUserIdEqualTo(userId);
		}
		if(state!=null){
			c.andStateEqualTo(state);
		}
		List<Address> list =addressMapper.selectByExample(example);
		if(list!=null&&list.size()>0){
			add =list.get(0);
		}
		return add;
	}

	@Override
	@Transactional(readOnly=true)
	public Address selectById(Integer id) {
		Address add =addressMapper.selectByPrimaryKey(id);
		return add;
	}

}
